package com.credithc.customers.dataservice.service.impl;

import com.credithc.baseserv.core.utils.UUIDUtils;
import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.base.mapper.CustomerAbnormalMapper;
import com.credithc.customers.base.mapper.CustomerContactFlowMapper;
import com.credithc.customers.base.po.CustomerAbnormalPo;
import com.credithc.customers.base.po.CustomerContactFlowPo;
import com.credithc.customers.dataservice.po.ServiceSystemUserInfo;
import com.credithc.customers.dataservice.service.ScheduleService;
import com.credithc.customers.dataservice.service.ServiceSystemUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhukai
 * @date 2019/1/4 13:35
 */
@Service
@Slf4j
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private CustomerAbnormalMapper abnormalMapper;
    @Autowired
    private CustomerContactFlowMapper contactFlowMapper;
    @Autowired
    private ServiceSystemUserService systemUserService;

    @Override
    public void insertDlrAbnormal() {
        Example example = new Example(CustomerContactFlowPo.class);
        example.setDistinct(false);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("modifyType", CustomerConstants.MODIFY_TYPE_ELEMENTS);
        criteria.andEqualTo("subSystemSign", CustomerConstants.DLR);
        criteria.andEqualTo("status", CustomerConstants.STATUS_TODO);//待处理(对证件信息变更)
        List<CustomerContactFlowPo> poList = contactFlowMapper.selectByExample(example);
        List<String> list1 = new ArrayList<>();
        for (CustomerContactFlowPo po : poList) {
            String customerNo = po.getCustomerNo();
            list1.add(customerNo);
        }
        List<ServiceSystemUserInfo> dlrList = systemUserService.getDLRListByCustomerNo(list1);
        for (CustomerContactFlowPo po : poList) {
            for (ServiceSystemUserInfo dlrPo : dlrList) {
                if (po.getCustomerNo().equals(dlrPo.getCustomerNo())) {
                    if (po.getCustomerName().equals(dlrPo.getName()) && po.getContactType().equals(dlrPo.getCardType()) && po.getContactInfo().equals(dlrPo.getCardNo())) {
                        //证件信息已修改，则修改变更表状态，否则记录异常信息到异常表
                        po.setStatus(CustomerConstants.STATUS_SUCCESS);
                        contactFlowMapper.updateByPrimaryKeySelective(po);
                    } else {
                        CustomerAbnormalPo abnormalPo = new CustomerAbnormalPo();
                        abnormalPo.setId(UUIDUtils.getUUID());
                        abnormalPo.setRequestNo(UUIDUtils.getUUID());
                        abnormalPo.setCreateTime(new Timestamp(System.currentTimeMillis()));
                        abnormalPo.setCustomerNo(dlrPo.getCustomerNo());
                        abnormalPo.setCustomerName(dlrPo.getName());
                        abnormalPo.setIdCardType(dlrPo.getCardType().toString());
                        abnormalPo.setIdCardNo(dlrPo.getCardNo());
                        abnormalPo.setSystemSign(CustomerConstants.DLR);
                        abnormalPo.setSubSystemSign(CustomerConstants.DLR);
                        //默认待处理状态
                        abnormalPo.setStatus(CustomerConstants.ABNORMAL_STATUS_TODO);
                        //异常类别-客户信息异常
                        abnormalPo.setAbnormalType(CustomerConstants.ABNORMAL_TYPE_DIFFERENT);
                        abnormalPo.setCreateTime(new Timestamp(System.currentTimeMillis()));
                        abnormalPo.setAssociatedId(po.getRequestNo());
                        abnormalMapper.insert(abnormalPo);
                    }
                }
            }
        }

    }
}
